package com.sdgs.app.utils;


import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 * HTTP请求工具类。
* @author shichengqun
 */
public class RequestUtil {
    
    /**
     * 判断是否ajax请求。
     * @param request 
     * @return isAjax
     */
    public static boolean isAjax(HttpServletRequest request) {
        return "XMLHttpRequest".equalsIgnoreCase(request.getHeader("X-Requested-With"));
    }
    
    /**
     * 获取request内的参数,并封装为Map返回(不支持数组形式的参数).
     * @author yinyanzhen
     * @param request request
     * @return map
     * @since V1.0.0
     */
    public static Map<String, String> getRequestParamMap(HttpServletRequest request) {
        //获取request内所有的参数名称
        Enumeration<String> paramNames = request.getParameterNames();
        Map<String, String> paramMap = new HashMap<String, String>();
        while (paramNames.hasMoreElements()) {
            String paramName = paramNames.nextElement();
            //参数名获取单个参数,封装入Map
            paramMap.put(paramName, request.getParameter(paramName));
        }
        return paramMap;
    }
}
